! VSemiletov, 07.2011
subroutine ReadContourData(name)
use Data_cfoam, only: rank, nump
use Acoustics
implicit none
include 'mpif.h'
    character(*),intent(in) :: name

    integer :: ContourFile, ierr
    integer :: i,j, n
    real(8),allocatable :: send(:),recv(:)
	character(20) :: line
    
    Contour3D = 0.0d0
    nPoints = 0
    if(rank.eq.0)then
        write(*,'(a20,a15)') name, ' is reading ...'
        
        ContourFile = 1
        open(ContourFile,file=name,iostat=ierr,action='read') ! open(ContourFile,FILE='ContourData.dat',iostat=ierr)
        if(ierr.ne.0) then
                write(*,*) '@@ Fatal error: ',name, ' open error!'
            stop '@@ Fatal error'
        end if
        
        do
            read(ContourFile,'(a20)',iostat=ierr) line
            if(ierr.lt.0)then ! EOF
                write(*,*) '@@ Fatal error: ',name, ' is incorrect!'
                stop '@@ Fatal error'
            end if
            if(ierr.gt.0)then ! error during the read execution
                write(*,*) '@@ Fatal error: ',name, ' is incorrect!'
                stop '@@ Fatal error'
            end if
            if(line(1:4).eq.'ZONE') exit
        end do

        do
            nPoints = nPoints + 1
            read(ContourFile,*,iostat=ierr) Contour3D(1:3,nPoints)
            if(ierr.lt.0)then ! EOF
                Contour3D(1:3,nPoints) = Contour3D(1:3,1)
                write(*,'(a20,a28)') name, ' has been read successfully!'
!                call AfterReadContourData()
                exit
            end if
            if(ierr.gt.0)then ! error during the read execution
                write(*,*) '@@ Fatal error: ',name, ' is incorrect!!'
                stop '@@ Fatal error'
            end if
!            write(*,*) nPoints, Contour3D(1:3,i)
            if(nPoints.eq.nMaxPoints)then ! 1000)then
                write(*,*) '@@ Fatal error: ',name, ' has more than', nMaxPoints, ' strings for coor of contour!'
                     ! 10000 strings for coor of contour!'
                stop '@@ Fatal error'
            end if
        end do
        close(ContourFile)
        
        open(ContourFile,file='Read'//trim(name),iostat=ierr)
        
        write(ContourFile,*) ' x y z'
        do i = 1,nPoints
            write(ContourFile,'(3f25.10)') Contour3D(1:3,i)
        end do
        
        close(ContourFile)
    end if

!    write(*,*) '@@ before Scatter1: nPoint=', nPoints, ' rank=', rank

    i = nPoints

!    call MPI_Barrier(MPI_COMM_WORLD,ierr)

!    call MPI_Scatter(i,1,MPI_INTEGER,nPoints,1,MPI_INTEGER,0,MPI_COMM_WORLD)
    call MPI_ALLREDUCE(i,nPoints,1,MPI_INTEGER,MPI_SUM,MPI_COMM_WORLD,ierr)

!    write(*,*) '@@ after Scatter1: nPoint=', nPoints, ' rank=', rank
    
	call MPI_Barrier(MPI_COMM_WORLD,ierr)


    n = 3*nPoints
!    if(rank.eq.0)then
        allocate(send(n))
        n = 0
        do j = 1,nPoints
            do i = 1,3
                n = n + 1
                send(n) = Contour3D(i,j)
            end do
        end do
!        allocate(recv(1))
!    else
        allocate(recv(n))
        recv = 0.0d0
!        allocate(send(1))
!    end if

    call MPI_ALLREDUCE(send,recv,n,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
!    call MPI_Scatter(send,n,MPI_DOUBLE_PRECISION,recv,n,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD)
	call MPI_Barrier(MPI_COMM_WORLD,ierr)

    if(rank.ne.0)then
        n = 0
        do j = 1,nPoints
            do i = 1,3
                n = n + 1
                Contour3D(i,j) = recv(n)
!                write(*,*) '@@ after Scatter3: Contour3D=', Contour3D(i,j), ' rank=', rank
            end do
        end do
        deallocate(send)
        deallocate(recv)
    else
        deallocate(send)
        deallocate(recv)
    end if

!    STOP 'HERRR'

    call AfterReadContourData()

end subroutine ReadContourData
